import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.Tuple;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
public class JedisDemo {
    static Jedis jedis;
    @BeforeAll
    public static void init(){
        jedis = new Jedis("192.168.56.77",6379);
    }
    @Test
    void t0(){
        String ret = jedis.ping("hello啊树哥");
        log.info("redis返回"+ret);
    }
    //使用String类型
    @Test
    void t1() throws InterruptedException {
        jedis.set("info","亚冬会");
        jedis.append("info","圆满成功");
        jedis.expire("info",3);
        TimeUnit.SECONDS.sleep(5);
        String info = jedis.get("info");
        log.info("获取到缓存的数据"+info);

    }
    @Test
    void t2() throws InterruptedException{
        jedis.del("fruitList");
        jedis.rpush("fruitList","苹果","香蕉","沙糖桔","梨");
        jedis.lpush("fruitList","苹果1","香蕉1","沙糖桔1","梨1");
        long n = jedis.lrem("fruitList",2,"梨");//删除元素并返回实际删除的个数
        List<String> fruitList = jedis.lrange("fruitList",0,-1);
        System.out.println(fruitList);
        System.out.println(jedis.llen("fruitList"));
    }
    @Test
    void t3() throws Exception{
        jedis.del("citySet");
        jedis.sadd("citySet","111","222","333","444","555","666","777");
        log.info("集合中成员个数{}",jedis.scard("citySet"));
        Set<String> set = jedis.smembers("citySet");
        //输出
        set.forEach(System.out::println);
        //判断集合中是否存在某个成员
        boolean exists = jedis.sismember("citySet","111");
        log.info("集合中 {} 111", exists ? "有":"没有");
    }
    @Test
    void t4() throws Exception{
        jedis.del("movieZSet");
        jedis.zadd("movieZSet",9.9,"哪吒");
        jedis.zadd("movieZSet",8.5,"唐探");
        jedis.zadd("movieZSet",8.0,"蛟龙");
        jedis.zadd("movieZSet",1.0,"封神");
        jedis.zadd("movieZSet",9.0,"熊出没");
        Set<String> set = jedis.zrevrange("movieZSet",0,-1);
        //输出
//        set.forEach(System.out::println);
        Set<Tuple> tupleSet = jedis.zrangeWithScores("movieZSet",0,-1);
        tupleSet.forEach(tuple -> {
            log.info("电影  {} ||评分  {}",tuple.getElement(),tuple.getScore());
        });
    }
    @Test
    void t5() throws Exception{
        jedis.del("s1");
        jedis.hset("s1","name","哪吒");
        jedis.hset("s1","teacher","太乙真人");
        jedis.hset("s1","weapon","12345");
        jedis.hset("s1","home","陈唐冠");
        //获取value
        String s1 = jedis.hget("s1","name");
        jedis.hgetAll("s1").forEach((k,v)->{
            log.info("field:{}.value:{}",k,v);
        });
    }
    //redis的消息订阅和发布
    @Test
    void t6() throws Exception{
        //当前线程订阅了三个redis频道（主题）
        jedis.subscribe(new JedisPubSub() {
            @Override
            //当其他线程在我订阅的channel下发布消息后，我会被唤醒，执行onMessage方法
            public void onMessage(String channel, String message) {
                log.info("接收到{}频道发布的消息，内容如下{}",channel,message);
            }
        },"Traffic","Sport","Music");
        log.info("当前线程睡眠状态，等待消息到来");
        TimeUnit.MINUTES.sleep(2);
    }
    @Test
    void t7() throws Exception{
        jedis.publish("Sport","123456s");
        jedis.publish("Music","1234567m");
    }
}
